package com.yst.web.utils;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public abstract class ExceptionUtils {

    private static final Map<Class<?>,Integer> depthCache=new ConcurrentHashMap<>();
    public static Integer getDepth(Throwable e){
        return depthCache.computeIfAbsent(e.getClass(),(key)->{
            Class<?> parent=key.getSuperclass();
            Integer result=1;
            while (Objects.nonNull(parent)){
                if (parent.equals(Throwable.class)){
                    break;
                }
                parent=parent.getSuperclass();
                result++;

            }
            return result;
        });
    }
    public static Integer getDepth(Class<? extends Throwable> e){
        return depthCache.computeIfAbsent(e,(key)->{
            Class<?> parent=key.getSuperclass();
            Integer result=1;
            while (Objects.nonNull(parent)){
                if (parent.equals(Throwable.class)){
                    break;
                }
                parent=parent.getSuperclass();
                result++;

            }
            return result;
        });
    }
}
